package com.atguigu.juc.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author yfstart
 * @create 2022-06-23 12:06
 */
public class CompletableFutureThreadPool {
  public static void main(String[] args) {
      ExecutorService threadPool = Executors.newFixedThreadPool(5);

      try {
          CompletableFuture<Void> completableFuture = CompletableFuture.supplyAsync(() -> {
              // try {TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}
              System.out.println("1号任务：" + Thread.currentThread().getName() + "\t ---come in");
              return "abc";
          },threadPool).thenRun(() -> {
              try {TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}
              System.out.println("2号任务：" + Thread.currentThread().getName() + "\t ---come in");
          }).thenRun(() -> {
              try {TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}
              System.out.println("3号任务：" + Thread.currentThread().getName() + "\t ---come in");
          });
        System.out.println(completableFuture.get(2L, TimeUnit.SECONDS));
      } catch (Exception e) {
          e.printStackTrace();
      } finally {
          threadPool.shutdown();
      }
  }
}
